package com.xseagull.gateway;

import java.text.ParseException;
/**
 * 对时间操作的油条
 */
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

	// 获取两个日期当中的每一天的日期
	public static List<String> getEveryDay(String dateFirst, String dateSecond) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		List<String> dateList = new ArrayList<>();
		if (dateFirst.equals(dateSecond)) {
			dateList.add(dateFirst);
			return dateList;
		}
		try {
			Date dateOne = dateFormat.parse(dateFirst);
			Date dateTwo = dateFormat.parse(dateSecond);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(dateOne);
			dateList.add(dateFirst);
			while (calendar.getTime().before(dateTwo)) {
				calendar.add(Calendar.DAY_OF_MONTH, 1);
				dateList.add(dateFormat.format(calendar.getTime()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dateList;

	}

	// 获取两个时间之间的小时整数(01:00-03:45==>{1,2,3})
	public static List<String> getEveryHour(String startTime, String endTime) {
		int start = Integer.parseInt(startTime.substring(0, 2));
		int end = Integer.parseInt(endTime.substring(0, 2));
		List<String> list = new ArrayList<String>();
		if (endTime.substring(3).equals("00")) {
			end = end - 1;
		}
		for (int i = start; i <= end; i++) {
			list.add(i + "");
		}
		return list;
	}

	// 两个时间获取小时数--16:00-18:15
	// 精度半个小时,多余半个小时算半个小时,少于半个小时取整
	public static double getHour(String startTime, String endTime) {
		try {
			SimpleDateFormat format = new SimpleDateFormat("HH:mm");
			long time = Math.abs(format.parse(endTime).getTime() - format.parse(startTime).getTime());
			double hour = time * 1.0 / 1000 / 3600;
			int hourInt = (int) hour;
			if (hour - hourInt >= 0.5) {
				return hourInt + 0.5;
			} else {
				return hourInt;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 获取两个日期之间的天数
	 * 
	 * @param smdate
	 * @param bdate
	 * @return
	 * @throws ParseException
	 */
	public static int daysBetween(String smdate, String bdate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			cal.setTime(sdf.parse(smdate));
			long time1 = cal.getTimeInMillis();
			cal.setTime(sdf.parse(bdate));
			long time2 = cal.getTimeInMillis();
			long between_days = (time2 - time1) / (1000 * 3600 * 24);
			return Math.abs(Integer.parseInt(String.valueOf(between_days)));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

}
